<template>
  <div class="main">
    <el-dialog title="指派小组长" width="80%" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="小组长" prop="leader">
          <el-select
          v-model="ruleForm.leader"
          placeholder="请选择小组长"
          style="width:50%;"
          >
            <el-option
              v-for="item in leaders"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
          </el-option>
        </el-select>
        </el-form-item>  
        <el-form-item label="备注信息" prop="notes">
          <el-input
            type="textarea"
            v-model="ruleForm.notes"
            maxlength="200"
            rows="3"
            show-word-limit
          ></el-input>
        </el-form-item>
        <!--子任务列表-->
        <el-form-item label="子任务">
          <el-row>
            <div class="m-wrap">
              <div class="work-title" style="width:25%;">子任务名称</div>
              <div class="work-title" style="width:45%;">工作内容描述</div>
              <div class="work-title" style="width:100px;">标准分</div>
              <div class="work-title" style="width:100px;">预计工时</div>
            </div>
          </el-row>
          <div
            class="m-wrap"
            v-for="(w, n) in works"
            :key="'w_' + n"
          >
          <el-input
            v-model="w.workName"
            placeholder="子任务名称" style="width:25%"
          ></el-input>
          <el-input
            type="textarea"
            v-model="w.workDetail"
            maxlength="400"
            placeholder="子任务内容描述" style="width: 45%;margin-left: 20px;"
            rows="1"
            show-word-limit
          ></el-input>
            <el-input-number
              v-model="w.scoreStd"
              :min="1"
              controls-position="right"
              placeholder="标准分"
              style="width: 100px;margin-left: 20px;"
            ></el-input-number>
            <el-input-number
              v-model="w.manHour"
              :min="1"
              controls-position="right"
              placeholder="预计工时"
              style="width: 100px;margin-left: 20px;"
            ></el-input-number>            
            <!-- 减 -->
            <el-button
              v-if="
                works.length > 1 &&
                works.length - 1 > n
              "
              icon="el-icon-minus"
              type="danger"
              size="mini"
              style="margin-left: 20px"
              circle
              @click="removeRow(n)"
            ></el-button>
            <!-- 加 -->
            <el-button
              v-if="
                n == works.length - 1 ||
                works.length == 1
              "
              icon="el-icon-plus"
              type="primary"
              size="mini"
              style="margin-left: 20px"
              circle
              @click="addRow(n)"
            ></el-button>
          </div>
        </el-form-item>
        <!---->
        <!-- -->
        <!-- <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { forwardTask,listLeaders, listTaskTypes } from "@/api/system/task";
export default {
  name: "addform",
  data() {
    return {
      placeholderTips: `1人月=22人天，1人天=${this.$store.state.user.appconfig.workTime}工时`,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      projectManagerArr: [],
      ruleForm: {},
      rules: {
        taskName: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
          { min: 1, max: 20, message: "长度在40 个字符以内", trigger: "blur" }
        ],
        leader: [
          { required: true, message: "请选择小组长", trigger: "blur" }
        ],
        startDate: [
          {
            type: "string",
            required: true,
            message: "请选择日期",
            trigger: "change"
          }
        ],
        scoreStd: [
          { required: true, message: "请输入标准分值", trigger: "blur" },
          { type: "number", message: "标准分必须为数字值" }
        ],
        manHour: [
          { required: true, message: "请输入工时", trigger: "blur" },
          { type: "number", message: "工时必须为数字值" }
        ],
        taskType: [
          { required: true, message: "请选择任务类别", trigger: "blur" }
        ]
      },
      options: [
        {
          value: "0",
          label: "人天"
        },
        {
          value: "1",
          label: "人月"
        }
      ],
      types: [             
      ],
      works: [],
      leaders:[],
      hourUnit: "0",
    };
  },
  created() {
    this.getTaskTypes();
    this.getLeaders();
  },
  methods: {
    showdate(value) {
      // console.log(value);
    },
    open() {
      this.dialogFormVisible = true;
      this.gettady();
    },
    closeForm() {
      this.resetForm("ruleForm");
      this.dialogFormVisible = false;
    },
    gettady() {
      let date = new Date();
      let yyyy = date.getFullYear();
      let mm = date.getMonth() + 1;
      let dd = date.getDate();
      if (mm < 10) {
        mm = "0" + mm;
      }
      if (dd < 10) {
        dd = "0" + dd;
      }
      let timetaday = yyyy + "-" + mm + "-" + dd;
      // console.log(timetaday);
      this.ruleForm.startDate = timetaday;
    },
    getTaskTypes(){
      listTaskTypes().then(res=>{
        if(res.code == 200){
          this.types = res.data;
        }
      });
    },
    getLeaders(){
      listLeaders().then(res=>{
        if(res.code == 200){
          this.leaders = res.data;
        }
      });
    },
    edithourUnit() {
      if (this.hourUnit == 0) {
        this.ruleForm.manHour =
          this.ruleForm.manHour * this.$store.state.user.appconfig.workTime;
        // console.log(this.ruleForm.manHour);
        return this.ruleForm.manHour;
      } else if (this.hourUnit == 1) {
        this.ruleForm.manHour =
          this.ruleForm.manHour *
          this.$store.state.user.appconfig.workTime *
          this.$store.state.user.appconfig.workDay;
        // console.log(this.ruleForm.manHour);
        return this.ruleForm.manHour;
      }
    },
    lookduration(value) {
      // 工时检验
      // console.log(value);
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // let temphour = this.ruleForm.manHour;
          this.ruleForm.temp = this.ruleForm.manHour;
          // console.log("temphour", this.ruleForm.temp);
          this.edithourUnit();
          // alert("submit!");
          //文件处理
        //   if(this.attachments && this.attachments.length>0){
        //     this.ruleForm.files=[];
        //     this.attachments.forEach(f=>{
        //       this.ruleForm.files.push({fileId:f.fileId, fileName: f.name, filePath: f.url, fileSize: f.size});
        //     });
        //   }
          // console.log(this.ruleForm);
          this.ruleForm.details = this.works;
          forwardTask(this.ruleForm)
            .then(res => {
              // console.log(res);
              if (res.code == 200) {
                this.resetForm(formName);
                this.dialogFormVisible = false;
                this.$parent.init();
              }
            })
            .catch(error => {
              //  if (res.code == 500) {
              this.ruleForm.manHour = this.ruleForm.temp;
              this.$message.warning("工时还原为" + this.ruleForm.temp);
              // console.log("temphour", this.ruleForm.temp);
              // }
              // console.log(error);
            });
            
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    addRow(n) {
        this.works.push({
          workName: this.ruleForm.taskName,
          workDetail: this.ruleForm.taskDetail,
          scoreStd: 5,
          manHour: 8
        });
    },
    removeRow(n) {
      this.works.splice(n, 1);
    },
  }
};
</script>

<style scoped lang="scss">
.tip{
  font-size: 12px;
    color: #606266;
    margin-top: 7px;
}
.work-title{
  text-align:center;float:left;margin-left: 20px;
}
</style>
